#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include "rnotify.h"

int main(int argc, char* argv[])
{
	if(argc != 2)
	{
		printf("Usage: a.out <dir>\n");
		exit(1);
	}

	const char* dir = argv[1];
	printf("Start to watch %s\n", dir);

	uint32_t mask = IN_MODIFY | IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF | IN_MOVED_FROM | IN_MOVED_TO;

	Notify* ntf = initNotify(dir, mask, "^\\.");
	if(ntf == NULL)
	{
		exit(EXIT_FAILURE);
	}

	for(;;)
	{
		char* path = NULL;
		uint32_t mask = 0;
		waitNotify(ntf, &path, &mask);
		if(path == NULL)
		{
			exit(EXIT_FAILURE);
		}
		if(mask & IN_MODIFY)
		{
			printf("modify \t%s\n", path);
		}
		if(mask & IN_CREATE)
		{
			printf("create \t%s\n", path);
		}
		if(mask & IN_DELETE)
		{
			printf("delete \t%s\n", path);
		}
		if(mask & IN_DELETE_SELF)
		{
			//printf("delete self %s\n", path);
		}
		if(mask & IN_MOVE_SELF)
		{
			printf("move self %s\n", path);
		}
		if(mask & IN_MOVED_FROM)
		{
			printf("moved from \t%s\n", path);
		}
		if(mask & IN_MOVED_TO)
		{
			printf("moved to \t%s\n", path);
		}
		if(mask & IN_Q_OVERFLOW)
		{
			printf("overflow\n");
		}
		if(!strlen(path))
		{
			exit(-1);
		}
		free(path);
	}
	freeNotify(ntf);
	return 0;
}

